package com.young.msg.service.impl;

import com.young.common.core.event.EventHelper;
import com.young.common.model.enums.ETrashFlagEnum;
import com.young.common.core.dal.service.IDataAccessService;
import com.young.common.util.Page;
import com.young.interfaces.log.annotation.Log;
import com.young.msg.model.MailSenderConfig;
import com.young.msg.model.enums.EMailStateEnum;
import com.young.msg.model.enums.EMsgEventPublisherEnum;
import com.young.msg.model.enums.EMsgEventDefinitionEnum;
import com.young.msg.service.IMsgMailSenderConfigService;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.Resource;
import java.util.List;

/**
* 邮件发送客户端服务实现
* Created by wangjiyu on 2021-10-28.
*/
@Service("mailSenderService")
public class MsgMailSenderConfigServiceImpl implements IMsgMailSenderConfigService {

    @Resource(name = "dataAccessService")
    IDataAccessService dataAccessService;//数据层服务

    private static final Logger logger = LoggerFactory.getLogger(MsgMailSenderConfigServiceImpl.class);

    private static final String MAPPER = "com.young.msg.mapper.mailSender";//mapper的namespace

    /**
     * 查询有效的邮箱配置
     *
     * @return
     */
    @Log("查询有效的邮箱配置")
    @Override
    public List<MailSenderConfig> getValidMailConfigList() {
        MailSenderConfig param = new MailSenderConfig();
        param.setTrashFlag(ETrashFlagEnum.VALID.type);
        param.setState(EMailStateEnum.ENABLE.type);
        return this.getList(param);
    }

    //获取数据列表
    @Log("查询邮箱配置列表")
    @Override
    public List<MailSenderConfig> getList(MailSenderConfig obj) {
        return dataAccessService.getList(MAPPER + ".getList", obj);
    }

    //获取数据列表(分页)
    @Log("分页查询邮箱配置列表")
    @Override
    public List<MailSenderConfig> getPage(MailSenderConfig obj, Page page) {
        return dataAccessService.getList(MAPPER + ".getList", obj, page);
    }

    //查询单条
    @Log("查询邮箱配置")
    @Override
    public MailSenderConfig get(Long id) {
        return dataAccessService.getObject(MAPPER + ".get", id);
    }

    /**
     * 根据标识查询邮箱配置
     *
     * @param senderCode 邮箱标识
     * @return
     */
    @Log("根据标识查询邮箱配置")
    @Override
    public MailSenderConfig getBySenderCode(String senderCode) {
        return dataAccessService.getObject(MAPPER + ".getBySenderCode", senderCode);
    }

    //统计数量
    @Log("计数邮箱配置")
    @Override
    public int count(MailSenderConfig obj) {
        return dataAccessService.getObject(MAPPER + ".count", obj);
    }

    //修改
    @Log("修改邮箱配置")
    @Override
    public int update(MailSenderConfig obj) {
        // 先查询
        MailSenderConfig config = this.get(obj.getId());
        if (config == null) {
            return 0;
        }
        // 再更新
        int num = dataAccessService.update(MAPPER + ".update", obj);
        if (num > 0) {
            // 发布事件
            EventHelper.publish(EMsgEventDefinitionEnum.MAIL_UPDATE, EMsgEventPublisherEnum.PUBLISHER_MSG_MAIL, config.getSenderCode());
        }
        return num;
    }

    //批量删除
    @Log("批量删除邮箱配置")
    @Override
    public int delete(Long[] idArr) {
        int i = 0;
        for(Long id : idArr){
            i += this.delete(id);
        }
        return i;
    }

    //删除单条
    @Log("删除邮箱配置")
    @Override
    public int delete(Long id) {
        // 先查询
        MailSenderConfig config = this.get(id);
        if (config == null) {
            return 0;
        }
        // 再删除
        int num = dataAccessService.delete(MAPPER + ".delete", id);
        if (num > 0) {
            // 发布事件
            EventHelper.publish(EMsgEventDefinitionEnum.MAIL_DELETE, EMsgEventPublisherEnum.PUBLISHER_MSG_MAIL, config.getSenderCode());
        }
        return num;
    }

    //插入
    @Log("插入邮箱配置")
    @Override
    public int insert(MailSenderConfig obj) {
        if (obj.getDataSequence() == null) {
            obj.setDataSequence(1L);
        }
        if (obj.getDataVersion() == null) {
            obj.setDataVersion(1L);
        }
        if (obj.getTrashFlag() == null) {
            obj.setTrashFlag(ETrashFlagEnum.VALID.type);
        }
        int num = dataAccessService.insert(MAPPER + ".insert", obj);
        if (num > 0) {
            // 发布事件
            EventHelper.publish(EMsgEventDefinitionEnum.MAIL_ADD, EMsgEventPublisherEnum.PUBLISHER_MSG_MAIL, obj.getSenderCode());
        }
        return num;
    }

    //批量插入
    @Log("批量插入邮箱配置")
    @Override
    public int batchInsert(List<MailSenderConfig> list) {
        int i = 0;
        for(MailSenderConfig item : list){
            i += this.insert(item);
        }
        return i;
    }

}